package mcmt.controller;

import java.io.File;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import mcmt.model.Item;
import mcmt.model.ReportItem;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XmlReportController extends ReportController {
	
	public void generateReport(List<ReportItem> items){
		try{
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	 
	    // root element
	    Document doc = docBuilder.newDocument();
	    Element rootElement = doc.createElement("report");
	    doc.appendChild(rootElement);

	    Element info = doc.createElement("info");
	    rootElement.appendChild(info);
	    
	    Element authors = doc.createElement("authors");
	    info.appendChild(authors);
	    
	    Element author = doc.createElement("author");
	    authors.appendChild(author);
	    
	    //first author
	    Element name = doc.createElement("name");
	    name.appendChild(doc.createTextNode("Zbyszko Natkanski"));
	    author.appendChild(name);
	    Element indexNumber = doc.createElement("index_number");
	    indexNumber.appendChild(doc.createTextNode("178614"));
	    author.appendChild(indexNumber);
	    Element department = doc.createElement("department");
	    department.appendChild(doc.createTextNode("IFE"));
	    author.appendChild(department);
	    
	    //second author
	    Element author1 = doc.createElement("author");
	    Element name1 = doc.createElement("name");
	    name1.appendChild(doc.createTextNode("Sebastian Pasiecznik"));
	    author1.appendChild(name1);
	    Element indexNumber1 = doc.createElement("index_number");
	    indexNumber1.appendChild(doc.createTextNode("178616"));
	    author1.appendChild(indexNumber1);
	    Element department1 = doc.createElement("department");
	    department1.appendChild(doc.createTextNode("IFE"));
	    author1.appendChild(department1);
	    authors.appendChild(author1);
	    
	    Element exchanges = doc.createElement("exchanges");
	    rootElement.appendChild(exchanges);
	    
	    for(ReportItem item : items){
		    // staff element
		    Element exchange = doc.createElement("exchange");
		    exchanges.appendChild(exchange);
		 
		    // set attribute to staff element
	//	    Attr attr = doc.createAttribute("id");
	//	    attr.setValue("1001");
	//	    exchange.setAttributeNode(attr);
		 
		    // shorten way
		    // staff.setAttribute("id", "1");
		    
		    Element input = doc.createElement("input_value");
		    input.appendChild(doc.createTextNode(Double.toString(item.getItem().getUnit())));
		    exchange.appendChild(input);
		    
		    Element inputCurr = doc.createElement("input_currency");
		    inputCurr.appendChild(doc.createTextNode(item.getInputCurrency().getCountryCode()));
		    exchange.appendChild(inputCurr);
		    
		    Element rate = doc.createElement("rate");
		    rate.appendChild(doc.createTextNode(Double.toString(item.getItem().getRate())));
		    exchange.appendChild(rate);

		    Element outputCurr = doc.createElement("output_currency");
		    outputCurr.appendChild(doc.createTextNode(item.getOutputCurrency().getCountryCode()));
		    exchange.appendChild(outputCurr);

		    Element outputValue = doc.createElement("output_value");
		    outputValue.appendChild(doc.createTextNode(Double.toString(item.getOutputValue())));
		    exchange.appendChild(outputValue);

		    Element title = doc.createElement("title");
		    title.appendChild(doc.createTextNode(item.getItem().getTitle()));
		    exchange.appendChild(title);
		    
		    Element link = doc.createElement("link");
		    link.appendChild(doc.createTextNode(item.getItem().getLink()));
		    exchange.appendChild(link);

		    Element region = doc.createElement("region");
		    region.appendChild(doc.createTextNode(item.getItem().getRegion()));
		    exchange.appendChild(region);

		    Element pubDate = doc.createElement("pubdate");
		    pubDate.appendChild(doc.createTextNode(item.getItem().getPubDate()));
		    exchange.appendChild(pubDate);
		    
		    Element description = doc.createElement("description");
		    description.appendChild(doc.createTextNode(item.getItem().getDescription()));
		    exchange.appendChild(description);
		}
	 
	    // write xml to file
	    TransformerFactory transformerFactory = TransformerFactory.newInstance();
	 
	    Transformer transformer = transformerFactory.newTransformer();
	    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
	 
	    DOMSource source = new DOMSource(doc);
	 
	    StreamResult result = new StreamResult(new File("file1.xml"));
	 
	    // Show output on console during development
	    //StreamResult result = new StreamResult(System.out);
	 
	    transformer.transform(source, result);
	 
	    System.out.println("Xml File saved!");
		} catch(Exception e){
			System.out.println("MESSAGE: "+e.getMessage());
		}
	}
}
